package com.css.fxfzdzzh.common.utils;

import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

public class ProjectTransUtil {

    private static GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
    private static WKTReader reader = new WKTReader(geometryFactory);

    private static String strWKTMercator = "PROJCS[\"CGCS2000_GK_Zone_20\",GEOGCS[\"GCS_China_Geodetic_Coordinate_System_2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137.0,298.257222101]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Gauss_Kruger\"],PARAMETER[\"False_Easting\",20500000.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",117.0],PARAMETER[\"Scale_Factor\",1.0],PARAMETER[\"Latitude_Of_Origin\",0.0],UNIT[\"Meter\",1.0]]";


    public static void setStrWKTMercator(String wKTMercator){
        strWKTMercator = wKTMercator;
    }

    /**
     * 经纬度转WEB墨卡托
     * @param geom
     * @return
     */
    public static Geometry lonlat2WebMactor(Geometry geom){
        try{
            //CoordinateReferenceSystem crsTarget = CRS.parseWKT(strWKTMercator);
            CoordinateReferenceSystem crsTarget = CRS.decode("EPSG:3857");
            // 投影转换
            MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, crsTarget);
            return JTS.transform(geom, transform);
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    public static Geometry webMercator2lonLat(Geometry geom){
        try{
            CoordinateReferenceSystem sourceTarget = CRS.parseWKT(strWKTMercator);
            CoordinateReferenceSystem crsTarget = CRS.decode("EPSG:4490");
            MathTransform transform = CRS.findMathTransform(sourceTarget, crsTarget, true);
            //MathTransform transform = CRS.findMathTransform(sourceTarget, DefaultGeographicCRS.WGS84, true);
            return JTS.transform(geom, transform);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String wkt2lonLat(String wktPolygon){
        Geometry geomtry = null;
        try {
            geomtry = reader.read(wktPolygon);
            geomtry = ProjectTransUtil.webMercator2lonLat(geomtry);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (geomtry != null)
            return geomtry.toString();
        else return null;
    }

    public static String wktPolygon2coordinates (String wktPolygon){
        StringBuilder result = new StringBuilder();
        try{
            Geometry geomtry = reader.read(wktPolygon);
            geomtry = ProjectTransUtil.webMercator2lonLat(geomtry);
            String str = "";
            int geoCount = geomtry.getNumGeometries();
            for (int idx = 0;idx < geoCount;idx++) {
                Geometry geo = geomtry.getGeometryN(idx);
                Coordinate[] coords = geo.getCoordinates();
                StringBuilder stringBuilder = new StringBuilder();
                for (Coordinate coord : coords) {
                    stringBuilder.append(coord);
                }
                str = stringBuilder.toString().replace(")(", " ");
                str = str.substring(1, str.length() - 1) + "|";
                str = str.replace(",NaN", "");
                result.append(str);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return result.toString();
    }


    public static String parseWktPolygon (String wktPolygon) {
        StringBuilder result = new StringBuilder();
        try{
            Geometry geomtry = reader.read(wktPolygon);
            String str;
            int count = geomtry.getNumGeometries();
            for (int idx = 0;idx < count;idx++) {
                Geometry geo = geomtry.getGeometryN(idx);
                Coordinate[] coords = geo.getCoordinates();
                StringBuilder stringBuilder = new StringBuilder();
                for (Coordinate coord : coords) {
                    stringBuilder.append(coord);
                }
                str = stringBuilder.toString().replace(")(", " ");
                str = str.replace(", ", ",");
                str = str.substring(1, str.length() - 1) + "|";
                str = str.replace(",NaN", "");
                result.append(str);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return result.toString();
    }

    public static void main(String[] args){

        String wktPolygon = "POLYGON ((-507925.35009999946 3766378.3257, -500728.66899999976 3778291.655099999, -496283.6601999998 3785117.9187000003, -490727.3990000002 3792102.9327000007, -485171.1378999986 3799405.4473, -480884.8794 3802421.7032999992, -474534.86670000106 3807184.2128999997, -469296.1061999984 3810359.2192, -453738.57510000095 3817502.9835, -438816.04520000145 3821947.9924, -425481.01850000024 3825599.2497000005, -403414.7243999988 3830838.0101999994, -393572.2047000006 3832743.0140000004, -379125.9257999994 3835283.019099999, -363250.8940999992 3837346.7731999997, -355630.878800001 3838458.0253999997, -343248.3541000001 3840363.0292000007, -335787.0892000012 3840839.280200001, -310387.0384000018 3840839.280200001, -286415.7404000014 3838934.2764, -273080.7137000002 3837823.0241, -248156.9138999991 3831314.2611, -236568.14070000127 3827980.5045, -218311.8542000018 3821947.9924, " +
                "-193335.13760000095 3810828.4146, -188360.9609999992 3808235.4926999994, -177936.35680000111 3802467.5645000003, -168781.75510000065 3797493.3879000004, -156028.813000001 3789979.2062, -145445.45850000158 3782994.1922999993, -138936.6955000013 3778496.2665999997, -129094.17579999939 3770400.0003999993, -125495.83520000055 3766219.5754000004, -120098.32440000027 3759128.7279000003, -116394.15040000156 3752884.548699999, -113536.6446999982 3746640.3696, -111367.05700000003 3740396.1904000007, -109620.80350000039 3732776.1752000004, -108403.71770000085 3724679.909, -106710.38100000098 3712085.7171, -106339.9635999985 3707905.2920999993, -105757.87909999862 3698062.772399999, -105175.79459999874 3689913.5894, -105599.128800001 3684727.7457, -106551.63069999963 3677954.3987999996, -106974.96489999816 3672133.5538999997, -107292.46550000086 3667053.5437000003, -107451.21579999849 3665836.4579, -108350.80099999905 3661814.7832000004, -110361.63830000162 3652871.8487, -111578.72410000116 3647421.4211, " +
                "-113272.06080000103 3641653.4929, -115441.6484999992 3634456.8119, -118934.15549999848 3625831.3778999997, -124384.58300000057 3616253.4420999996, -130681.67889999971 3605934.6715, -135179.60460000113 3599372.9917, -140312.5315000005 3592282.1442, -148461.71449999884 3584820.8792000003, -155922.97940000147 3577730.0317, -162643.40949999914 3574502.1086, -167723.41970000044 3571856.2699999996, -178095.10709999874 3568840.0140000004, -185768.03909999877 3567358.3443, -198415.14770000055 3567040.8437, -208575.16809999943 3567887.512, -218311.8542000018 3569210.4314, -228524.79129999876 3570427.5171, -240642.73220000044 3572755.8551000003, -245934.40949999914 3573549.6067000004, -253236.9241000004 3574819.6092, -258634.4349000007 3575877.9447, -261386.10700000077 3576671.6963, -265936.94949999824 3578206.2827000003, -271969.4615000002 3580111.2865000004, -281282.8134999983 3582968.7922, -307370.78229999915 3591329.6423000004, -326262.07009999827 3597362.1542999996, -345047.52430000156 3604082.5844, " +
                "-362192.55860000104 3610591.3475, -369653.82360000163 3613448.8532, -381083.84640000015 3617364.6942999996, -404737.6436999999 3625143.4599, -424422.6831 3631599.3060999997, -447706.063000001 3639801.4058999997, -468502.3546000011 3646945.1701999996, -488187.39400000125 3654406.4351000004, -499088.24909999967 3659063.1111000003, -513164.1105999984 3666630.2095, -520995.79289999977 3672768.5551000005, -526075.8031000011 3678324.8163, -528668.7248999998 3683775.2438000003, -532319.9822000004 3691924.4267999995, -532214.1486999989 3697533.604699999, -531684.9809999987 3703619.033500001, -531261.6468000002 3706529.4560000002, -528562.8913999982 3715049.056399999, -524964.5507999994 3726849.4966, -520307.8748999983 3737432.8510999996, -517873.70329999924 3742936.1954999994, -514328.27959999815 3751349.9623000007, -512370.3590000011 3756429.9725, -507925.35009999946 3766378.3257))";

        //String wktPolygon = "MULTIPOLYGON (((-507925.35009999946 3766378.3257, -500728.66899999976 3778291.655099999, -496283.6601999998 3785117.9187000003, -490727.3990000002 3792102.9327000007, -485171.1378999986 3799405.4473, -480884.8794 3802421.7032999992, -474534.86670000106 3807184.2128999997, -469296.1061999984 3810359.2192, -453738.57510000095 3817502.9835, -438816.04520000145 3821947.9924, -425481.01850000024 3825599.2497000005, -403414.7243999988 3830838.0101999994, -393572.2047000006 3832743.0140000004, -379125.9257999994 3835283.019099999, -363250.8940999992 3837346.7731999997, -355630.878800001 3838458.0253999997, -343248.3541000001 3840363.0292000007, -335787.0892000012 3840839.280200001, -310387.0384000018 3840839.280200001, -286415.7404000014 3838934.2764, -273080.7137000002 3837823.0241, -248156.9138999991 3831314.2611, -236568.14070000127 3827980.5045, -218311.8542000018 3821947.9924, -193335.13760000095 3810828.4146, -188360.9609999992 3808235.4926999994, -177936.35680000111 3802467.5645000003, -168781.75510000065 3797493.3879000004, -156028.813000001 3789979.2062, -145445.45850000158 3782994.1922999993, -138936.6955000013 3778496.2665999997, -129094.17579999939 3770400.0003999993, -125495.83520000055 3766219.5754000004, -120098.32440000027 3759128.7279000003, -116394.15040000156 3752884.548699999, -113536.6446999982 3746640.3696, -111367.05700000003 3740396.1904000007, -109620.80350000039 3732776.1752000004, -108403.71770000085 3724679.909, -106710.38100000098 3712085.7171, -106339.9635999985 3707905.2920999993, -105757.87909999862 3698062.772399999, -105175.79459999874 3689913.5894, -105599.128800001 3684727.7457, -106551.63069999963 3677954.3987999996, -106974.96489999816 3672133.5538999997, -107292.46550000086 3667053.5437000003, -107451.21579999849 3665836.4579, -108350.80099999905 3661814.7832000004, -110361.63830000162 3652871.8487, -111578.72410000116 3647421.4211, -113272.06080000103 3641653.4929, -115441.6484999992 3634456.8119, -118934.15549999848 3625831.3778999997, -124384.58300000057 3616253.4420999996, -130681.67889999971 3605934.6715, -135179.60460000113 3599372.9917, -140312.5315000005 3592282.1442, -148461.71449999884 3584820.8792000003, -155922.97940000147 3577730.0317, -162643.40949999914 3574502.1086, -167723.41970000044 3571856.2699999996, -178095.10709999874 3568840.0140000004, -185768.03909999877 3567358.3443, -198415.14770000055 3567040.8437, -208575.16809999943 3567887.512, -218311.8542000018 3569210.4314, -228524.79129999876 3570427.5171, -240642.73220000044 3572755.8551000003, -245934.40949999914 3573549.6067000004, -253236.9241000004 3574819.6092, -258634.4349000007 3575877.9447, -261386.10700000077 3576671.6963, -265936.94949999824 3578206.2827000003, -271969.4615000002 3580111.2865000004, -281282.8134999983 3582968.7922, -307370.78229999915 3591329.6423000004, -326262.07009999827 3597362.1542999996, -345047.52430000156 3604082.5844, -362192.55860000104 3610591.3475, -369653.82360000163 3613448.8532, -381083.84640000015 3617364.6942999996, -404737.6436999999 3625143.4599, -424422.6831 3631599.3060999997, -447706.063000001 3639801.4058999997, -468502.3546000011 3646945.1701999996, -488187.39400000125 3654406.4351000004, -499088.24909999967 3659063.1111000003, -513164.1105999984 3666630.2095, -520995.79289999977 3672768.5551000005, -526075.8031000011 3678324.8163, -528668.7248999998 3683775.2438000003, -532319.9822000004 3691924.4267999995, -532214.1486999989 3697533.604699999, -531684.9809999987 3703619.033500001, -531261.6468000002 3706529.4560000002, -528562.8913999982 3715049.056399999, -524964.5507999994 3726849.4966, -520307.8748999983 3737432.8510999996, -517873.70329999924 3742936.1954999994, -514328.27959999815 3751349.9623000007, -512370.3590000011 3756429.9725, -507925.35009999946 3766378.3257)))";
        //String wktPolygon = "MULTIPOLYGON (((89118.76289999858 3831353.6537999995, 91447.10080000013 3833787.8254000004, 94357.52329999954 3837068.6653000005, 95521.6922999993 3837756.5833, 99649.20059999824 3840084.9212999996, 102347.95600000024 3840931.5897000004, 106581.29780000076 3841460.7574000005, 110973.3898999989 3841460.7574000005, 118064.23739999905 3840402.4219000004, 119175.48959999904 3839608.6702999994, 120657.15929999948 3838867.8355, 122244.6623999998 3838391.5845999997, 125260.91849999875 3836856.9981999993, 127430.50609999895 3834581.5769999996, 129494.26029999927 3830824.4860999994, 130764.26280000061 3828601.9816999994, 132457.59950000048 3824633.2237, 133515.93499999866 3821987.3850999996, 134733.0207999982 3816536.9574999996, 137960.9439000003 3803307.7643999998, 139707.19739999995 3797010.6685000006, 140395.1154000014 3794841.0808000006, 141135.95019999892 3792089.4086000007, 141400.53409999982 3791083.99, 142829.28689999878 3786691.8978000004, 143675.95529999956 3783463.9747, 145263.45850000158 3777537.2961999997, 146215.96040000021 3776055.626599999, 149920.13450000063 3766848.1082000006, 151825.13830000162 3761715.1811999995, 154100.55950000137 3755365.1685000006, 155635.1458999999 3749808.907400001, 156534.73099999875 3748327.2378000002, 159339.3200000003 3742982.6437999997, 162461.40949999914 3736632.631100001, 166906.41840000078 3728748.0319999997, 175426.0188000016 3720493.0154999997, 183046.03400000185 3712396.7492999993, 189607.71379999816 3704247.566299999, 192359.38599999994 3698585.4716, 193364.80469999835 3695304.6317, 194952.30790000036 3689589.6202999996, 195746.05939999968 3684615.4436999997, 196381.06069999933 3682075.4386, 196169.3935999982 3680276.2684000004, 195957.7265000008 3676625.011, 195640.22590000182 3670327.9151, 195269.80849999934 3668528.7449000003, 192623.96990000084 3661914.1482999995, 190824.79960000142 3658262.891, 184210.2030000016 3646832.8680999996, 181670.19799999893 3644345.7797999997, 178918.52580000088 3641752.858, 173573.93180000037 3637572.4329000004, 160662.2393000014 3629158.6661, 151348.8872999996 3623549.4881999996, 143675.95529999956 3620004.0645000003, 128012.59059999883 3612860.3002000004, 125895.91970000044 3612119.4654, 113460.47819999978 3607621.5396999996, 103459.20820000023 3604023.1991999997, 88377.92799999937 3600213.1915999996, 76947.90520000085 3599260.6897, 67740.38679999858 3602911.9469999997, 62977.87719999999 3613177.8008000003, 62872.04369999841 3614236.1363000004, 63030.79399999976 3619316.1464, 62713.29340000078 3629846.5842000004, 62660.37660000101 3635349.9285000004, 63136.62759999931 3643922.4456, 63454.12819999829 3649425.79, 64194.96299999952 3658897.8923000004, 64300.79659999907 3664612.9036999997, 64829.96429999918 3668793.3287000004, 65200.38170000166 3671915.4183, 65729.54940000176 3679958.7676999997, 66417.46750000119 3686732.1146, 67369.96939999983 3696204.2169000003, 68428.30480000004 3708851.3255000003, 68692.88870000094 3712925.9169999994, 69274.97320000082 3719328.8465, 70068.72480000183 3725414.2753, 70227.47509999946 3729330.1164999995, 70439.14220000058 3731446.7874, 70756.64279999956 3735468.462099999, 71179.97700000182 3737532.2161999997, 71603.31120000035 3741395.1405999996, 72661.64660000056 3747004.318499999, 73190.81430000067 3751978.495100001, 73719.98209999874 3756688.0878, 74884.1510999985 3764678.5205000006, 75783.73620000109 3773039.3705, 76736.23809999973 3781294.3871, 77000.82200000063 3783463.9747, 77794.57349999994 3789496.4868, 78006.24060000107 3791560.2409000006, 78747.07539999858 3795581.9156, 79170.40960000083 3798280.671, 79911.24439999834 3803466.5146999992, 81445.83080000058 3811351.1138000004, 82027.91530000046 3814737.7873, 83086.25079999864 3820082.3813000005, 83668.33529999852 3820876.1328999996, 84303.33659999818 3822516.5527999997, 85996.67330000177 3826273.6437, 87055.00869999826 3828178.647500001, 88325.0113000013 3829766.150699999, 89118.76289999858 3831353.6537999995)))";
        //String wktPolygon = "MULTIPOLYGON (((1140196.1308999993 3832937.4856000002, 1152080.5324999988 3837468.5724, 1166844.311999999 3842231.0819000006, 1169754.7344999984 3842442.749, 1190974.3603000008 3843712.751599999, 1199811.4613000005 3843765.668299999, 1201557.7148000002 3843554.0012, 1205208.9721000008 3843130.6670999993, 1207748.9770999998 3842866.0832, 1211558.9847999997 3842495.6657999996, 1213728.5724 3842178.1652000006, 1215739.4098000005 3841543.163899999, 1219919.8348000012 3840220.2446, 1236800.2851999998 3835140.2344000004, 1244579.0507999994 3830801.0591, 1252569.4835 3824768.5470000003, 1255321.1556000002 3819106.452299999, 1258919.496199999 3809084.0155999996, 1259399.9803999998 3796756.5242999997, 1259611.647500001 3789009.5088, 1260094.9541000016 3770760.277899999, 1258412.2007 3760505.0074000005, 1256919.9477000013 3752757.9919000007, 1255829.8621999994 3742386.3045000006, 1254348.1926000006 3728257.5262, 1252813.6061999984 3719314.5917000007, 1250485.268199999 3704550.8121000007, 1250114.8508000001 3702910.3922000006, 1248527.3476000018 3697936.2156000007, 1244029.4219000004 3684019.1043999996, 1241595.2503999993 3675605.3376, 1237256.075100001 3663381.5631, 1229900.6437 3643643.607, 1217253.5350000001 3627345.2410000004, 1213655.1944999993 3622582.7314999998, 1205347.2611999996 3617555.6381, 1203759.7580999993 3617132.3039999995, 1200478.9182000011 3616656.0530000003, 1192065.1512999982 3615174.3833999997, 1185080.1374000013 3614433.5486000003, 1172327.1952 3613481.0467, 1167564.6856999993 3613533.9634, 1145445.4748000018 3614751.0492000002, 1134608.1198000014 3617333.3877, 1129993.7771999985 3618391.7231, 1126564.770300001 3619280.7249, 1119283.4224000014 3628890.4107999997, 1113737.7446999997 3637822.762, 1112213.7415999994 3643453.1065999996, 1109927.7371000014 3651792.7899, 1108446.067400001 3657169.1339999996, 1106117.7294000015 3664831.4826999996, 1105059.3940000013 3671181.4954000004, 1103408.3907000013 3682018.8504, 1102223.0549999997 3689892.8661, 1101418.7199999988 3695184.543400001, 1101207.0529999994 3699544.885399999, 1100995.3858999982 3708985.2377000004, 1100656.7184999995 3717705.9218000006, 1100953.0524000004 3724775.602600001, 1101545.7203000002 3732353.2843999993, 1102434.7221000008 3741751.303200001, 1103112.0568000004 3749074.9845000003, 1104043.392000001 3756440.9991999995, 1105440.3948 3767024.353700001, 1107938.066399999 3775406.3705, 1110901.4056999981 3786328.3923000004, 1111536.4068999998 3787556.0615, 1113483.7441999987 3792297.4043000005, 1116658.750500001 3799663.4189999998, 1122797.0960999988 3812659.7783000004, 1124575.0997000001 3815284.4503000006, 1129909.1103999987 3821719.1297999993, 1133676.7846000008 3826291.1389000006, 1136597.7903999984 3829127.4779000003, 1140196.1308999993 3832937.4856000002)), " +
        //"((89118.76289999858 3831353.6537999995, 91447.10080000013 3833787.8254000004, 66417.46750000119 3686732.1146, 89118.76289999858 3831353.6537999995)))";

        try {
            long start = System.currentTimeMillis();

            String coords = ProjectTransUtil.wktPolygon2coordinates(wktPolygon);
            //System.out.println("coords: ");
            //System.out.println(coords);

            //System.out.println("坐标转换完成，共耗时"+(System.currentTimeMillis() - start)+"ms");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }


}
